﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Threading.Tasks;
namespace StandardStorage
{
    public class GlobalStorageClass : MonoBehaviour
    {
        public Dictionary<string, Action<UnityEngine.Object>> globalMethodActions = new Dictionary<string, Action<UnityEngine.Object>>();
        public delegate R GlobalMethodDelegate<T, R>(T obj) where T : UnityEngine.Object;
    }

    /// <summary>
    /// 存储异步方法
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class AsyncMethodCollection<T>
    {
        private Dictionary<string, Func<T, Task<string>>> methods = new Dictionary<string, Func<T, Task<string>>>();

        public AsyncMethodCollection(int size)
        {
            // 这里 size 参数暂时未使用，可根据需求添加逻辑
        }

        public Func<T, Task<string>> this[string key]
        {
            get
            {
                if (methods.TryGetValue(key, out var method))
                {
                    return method;
                }
                return null;
            }
            set
            {
                methods[key] = value;
            }
        }
    }



    /// <summary>
    /// 存储Action类
    /// </summary>
    /// <typeparam name="T"></typeparam>


    public class MethodCollection<T> //where T : new()
    {
        private Action<T>[] array;

        public Dictionary<string, Action<T>> thisAction = new Dictionary<string, Action<T>>();

        public MethodCollection(int size)
        {
            array = new Action<T>[size];
        }

        // 定义索引器 
        public Action<T> this[int index, string str]
        {
            get
            {
                if (index < 0 || index >= array.Length)
                {
                    throw new IndexOutOfRangeException("索引超出范围");
                }
                return array[index];
            }
            set
            {
                if (index < 0 || index >= array.Length)
                {
                    throw new IndexOutOfRangeException("索引超出范围");
                }
                array[index] = value;
                if (!thisAction.ContainsKey(str))
                {
                    thisAction.Add(str, value);
                }
            }
        }

        public Action<T> this[string str]
        {
            get
            {
                if (thisAction.ContainsKey(str))
                {
                    return thisAction[str];
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (!thisAction.ContainsKey(str))
                {
                    thisAction.Add(str, value);
                }

            }
        }

        public int Length
        {
            get { return array.Length; }
        }
    }


    /// <summary>
    /// 存储协程方法
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class IECollection<T>
    {
        private T[] array;

        public Dictionary<string, T> thisAction = new Dictionary<string, T>();


        public IECollection(int size)
        {
            array = new T[size];
        }

        // 定义索引器 
        public T this[int index, string str]
        {
            get
            {
                if (index < 0 || index >= array.Length)
                {
                    throw new IndexOutOfRangeException("索引超出范围");
                }
                return array[index];
            }
            set
            {
                if (index < 0 || index >= array.Length)
                {
                    throw new IndexOutOfRangeException("索引超出范围");
                }
                array[index] = value;
                if (!thisAction.ContainsKey(str))
                {
                    thisAction.Add(str, value);
                }
            }
        }

        public T this[string str]
        {
            get
            {
                if (thisAction.ContainsKey(str))
                {
                    return thisAction[str];
                }
                else
                {
                    T t = default(T);
                    return t;
                }
            }
            set
            {
                if (!thisAction.ContainsKey(str))
                {
                    thisAction.Add(str, value);
                }

            }
        }

        public int Length
        {
            get { return thisAction.Count; }
        }
    }
    

    public class Tang<T> : System.Collections.IEnumerable
    {
        private List<T> _list = new List<T>();
        public T this[int index] {
            get {
                if (index >= 0 && index < _list.Count) {
                    return _list[index];
                }
                return default(T);
            }
            set {
                if (index >= 0) {
                    while (index >= _list.Count) {
                        _list.Add(default(T));
                    }
                    _list[index] = value;
                }
            }
        }
        public void Add(T item)
        {
            _list.Add(item);
        }
        public IEnumerator GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}